package com.app.manage.moudles.server.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.app.manage.moudles.server.constant.ServerStatusEnums;
import com.app.manage.moudles.server.entity.TServer;
import com.app.manage.moudles.server.service.TServerService;
import com.app.manage.utils.PageSearch;
import com.app.manage.utils.Rsp;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.util.*;

@RestController
@RequestMapping("server")
@Api(tags = "服务器管理")
public class ServerController {

    @Resource
    private TServerService tServerService;


    @GetMapping("listServer")
    @ApiOperation("服务器列表")
    public Rsp listServer(){
        return Rsp.success(tServerService.list());
    }


    @GetMapping("findServerStatus")
    @ApiOperation("查询服务器状态")
    public Rsp  findServerStatus(){
        List<Map<String,Object>> retList = new ArrayList<>();
        for(ServerStatusEnums s : ServerStatusEnums.values()){
            retList.add(new HashMap<String,Object>(){{
                put("name",s.getName());
                put("status",s.getStatus());
            }});
        }
        return Rsp.success(retList);
    }


    @PostMapping("checkConnect")
    @ApiOperation("检查服务器连接")
    public Rsp checkConnect(@RequestBody TServer t){
        Map<String,Object> retMap = new HashMap<>();

        t.setServerIp(StrUtil.trim(t.getServerIp()));
        String res = tServerService.checkSshConnect(t);
        if(StringUtils.isNotEmpty(res)){
            retMap.put("code","500");
            retMap.put("status",ServerStatusEnums.S_C.getStatus());
            if(res.contains("Auth fail")){
                retMap.put("msg","请检测账号、密码是否正确！");
            }else if(res.contains("socket is not established")){
                retMap.put("msg","请检查"+t.getServerIp()+"是否开启防火墙");
            }else if(res.contains("Connection refused")){
                retMap.put("msg","连接超时！");
            }else {
                retMap.put("msg","服务器连接异常");
            }
        }else {
            retMap.put("status",ServerStatusEnums.H_C.getStatus());
            retMap.put("code","200");
            retMap.put("msg","连接成功");
        }
        return Rsp.success(retMap);
    }


    @PostMapping("pageList")
    @ApiOperation("分页查询服务器信息")
    public Rsp  page(@RequestBody PageSearch<TServer> s){
        return Rsp.success(tServerService.pageData(s.getPageNum(),s.getPageSize(),s.getDomain()));
    }

    @PostMapping("save")
    @ApiOperation("保存")
    public Rsp save(@RequestBody TServer tServer){
        tServer.setServerIp(StrUtil.trim(tServer.getServerIp()));
        String res = tServerService.saveData(tServer);
        if(StringUtils.isNotEmpty(res)){
            return Rsp.fail(res);
        }
        return Rsp.success("新增完成");
    }

    @GetMapping("getById")
    @ApiOperation("根据Id查询")
    public Rsp  getById(Integer id){
        return Rsp.success(tServerService.getById(id));
    }




    @PostMapping("remove")
    @ApiOperation("删除")
    public Rsp remove(String ids){
        if(StringUtils.isEmpty(ids)){
            return Rsp.fail("请选择需要删除的数据！");
        }
        String res =  tServerService.removeIds(Arrays.asList(ids.split(",")));
        if(StringUtils.isNotEmpty(res)){
            return Rsp.fail(res);
        }
        return Rsp.success("删除完成");
    }

    @PostMapping("changeStatus")
    public Rsp changeStatus(@RequestBody JSONObject obj){
        Integer oldStatus = obj.getInt("status");
        Integer id = obj.getInt("id");
        tServerService.update(new LambdaUpdateWrapper<TServer>()
                .set(TServer::getServerStatus,oldStatus)
                .eq(TServer::getId,id)
        );
        return Rsp.success();
    }

    @GetMapping("checkAndUpdStatus")
    public Rsp checkStatus(){
        return Rsp.success(tServerService.checkAndUpdStatus());
    }


    @PostMapping("filePath")
    public Object  filePath(Integer serverId,String  prePath,String allPath){

        if(StringUtils.isEmpty(prePath) || StringUtils.isEmpty(allPath)){
            prePath = allPath = "/";
        }
        return Rsp.success(tServerService.listNodePo(serverId,prePath,allPath));
    }

}
